#include "qresourcemanager.h"

#include "qresourcefile.h"

#include <QDir>
#include <QFileInfo>

using namespace SHAREDLIB;

QResourceManager::QResourceManager(QObject *parent) :
    QObject(parent)
{
}

void QResourceManager::addResource(const QString &firstName, const QString &lastName, const QByteArray &data)
{
    QResourceFile *file=m_nameToFile.value(firstName);
    if(file==NULL)
    {
        file=new QResourceFile;
        file->setFileName(firstName);
        m_files.append(file);
        m_nameToFile.insert(firstName,file);
    }
    file->addResource(lastName,data);
    emit resourceAdd(firstName,lastName);
}

void QResourceManager::removeResource(const QString &firstName, const QString &lastName)
{
    QResourceFile *file=m_nameToFile.value(firstName);
    if(file!=NULL)
    {
        emit resourceRemove(firstName,lastName);
        if(lastName!="")
        {
            file->removeResource(lastName);
        }
        else
        {
            m_files.removeAll(file);
            m_nameToFile.remove(firstName);
            delete file;
        }
    }
}

QStringList QResourceManager::resourceFirstNames()
{
    return m_nameToFile.keys();
}

QStringList QResourceManager::resourceLastNames(const QString &firstName)
{
    QResourceFile *file=m_nameToFile.value(firstName);
    if(file!=NULL)
    {
        return file->resourceNames();
    }
    return QStringList();
}

QStringList QResourceManager::resourceLastNames(int index)
{
    if(index>=0 && index<m_files.size())
    {
        return m_files.at(index)->resourceNames();
    }
    return QStringList();
}

QByteArray QResourceManager::resourceData(const QString &firstName, const QString &lastName)
{
    QResourceFile *file=m_nameToFile.value(firstName);
    if(file!=NULL)
    {
        QResourceInfo *info=file->resource(lastName);
        if(info!=NULL)
        {
            return info->resourceData();
        }
    }
    return QByteArray();
}

void QResourceManager::clear()
{
    qDeleteAll(m_files);
    m_files.clear();
    m_nameToFile.clear();
}

void QResourceManager::save(const QString &path)
{
    foreach(QResourceFile *file,m_files)
    {
        file->save(path);
    }
}

void QResourceManager::save(const QString &path, const QString &firstName)
{
    QResourceFile *file=m_nameToFile.value(firstName);
    if(file!=NULL)
    {
        file->save(path);
    }
}

void QResourceManager::load(const QString &path)
{
    QDir dir(path);
    QList<QFileInfo> infos=dir.entryInfoList();
    QString str;
    foreach(QFileInfo info,infos)
    {
        str=info.fileName();
        if(str.endsWith(".rus"))
        {
            str=str.left(str.size()-4);
            QResourceFile *file=new QResourceFile;
            file->setFileName(str);
            if(file->load(path))
            {
                m_files.append(file);
                m_nameToFile.insert(str,file);
            }
            else
            {
                delete file;
            }
        }
    }
}
